using UnityEngine;

public class skidMarks : MonoBehaviour
{
	public float bumpMinForce = 4000f;

	public float bumpMaxForse = 18000f;

	private float bumpMinVolume = 0.2f;

	private float bumpMaxVolume = 0.6f;

	public car car1;

	public float minToSlip = 150f;

	public bool rear;

	public float skidAt = 5.5f;

	public float markWidth = 0.2f;

	public Material skidMaterial;

	public GameObject skidSmoke;

	public float currentfrictionValue;

	private int skidding;

	private Vector3[] lastPos = new Vector3[2];

	private WheelHit hit;

	private float force;

	private float stress;

	private bool enableEmission;

	private ParticleSystem.EmissionModule em;

	public bool skid;

	public bool bump;

	public float bumpPitch;

	public float bumpVolume;

	private void Start()
	{
		em = skidSmoke.GetComponent<ParticleSystem>().emission;
	}

	private void Update()
	{
		if (car1.controlled)
		{
			base.transform.GetComponent<WheelCollider>().GetGroundHit(out hit);
			currentfrictionValue = Mathf.Abs(hit.sidewaysSlip);
			stress = force - hit.force;
			force = hit.force;
			BumpPlay(stress);
			float rpm = base.transform.GetComponent<WheelCollider>().rpm;
			if (skidAt <= currentfrictionValue || (rpm < minToSlip && UnityEngine.Input.GetAxis("Vertical") > 0f && rear && (bool)hit.collider))
			{
				SkidMesh();
				skid = true;
			}
			else
			{
				skidding = 0;
				enableEmission = false;
				skid = false;
			}
			if (base.transform.GetComponentInParent<Rigidbody>().velocity.magnitude > 5f)
			{
				enableEmission = true;
			}
			else
			{
				enableEmission = false;
			}
			if (skidSmoke != null)
			{
				em.enabled = enableEmission;
			}
		}
	}

	private void SkidMesh()
	{
		GameObject gameObject = new GameObject("Mark");
		MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
		gameObject.AddComponent<MeshRenderer>();
		Mesh mesh = new Mesh();
		Vector3[] array = new Vector3[4];
		int[] array2 = new int[6];
		if (skidding == 0)
		{
			ref Vector3 reference = ref array[0];
			Vector3 point = hit.point;
			Vector3 eulerAngles = base.transform.eulerAngles;
			float x = eulerAngles.x;
			Vector3 eulerAngles2 = base.transform.eulerAngles;
			float y = eulerAngles2.y;
			Vector3 eulerAngles3 = base.transform.eulerAngles;
			reference = point + Quaternion.Euler(x, y, eulerAngles3.z) * new Vector3(markWidth, 0.01f, 0f);
			ref Vector3 reference2 = ref array[0];
			Vector3 point2 = hit.point;
			Vector3 eulerAngles4 = base.transform.eulerAngles;
			float x2 = eulerAngles4.x;
			Vector3 eulerAngles5 = base.transform.eulerAngles;
			float y2 = eulerAngles5.y;
			Vector3 eulerAngles6 = base.transform.eulerAngles;
			reference2 = point2 + Quaternion.Euler(x2, y2, eulerAngles6.z) * new Vector3(0f - markWidth, 0.01f, 0f);
			ref Vector3 reference3 = ref array[2];
			Vector3 point3 = hit.point;
			Vector3 eulerAngles7 = base.transform.eulerAngles;
			float x3 = eulerAngles7.x;
			Vector3 eulerAngles8 = base.transform.eulerAngles;
			float y3 = eulerAngles8.y;
			Vector3 eulerAngles9 = base.transform.eulerAngles;
			reference3 = point3 + Quaternion.Euler(x3, y3, eulerAngles9.z) * new Vector3(0f - markWidth, 0.01f, 0f);
			ref Vector3 reference4 = ref array[3];
			Vector3 point4 = hit.point;
			Vector3 eulerAngles10 = base.transform.eulerAngles;
			float x4 = eulerAngles10.x;
			Vector3 eulerAngles11 = base.transform.eulerAngles;
			float y4 = eulerAngles11.y;
			Vector3 eulerAngles12 = base.transform.eulerAngles;
			reference4 = point4 + Quaternion.Euler(x4, y4, eulerAngles12.z) * new Vector3(markWidth, 0.01f, 0f);
			lastPos[0] = array[2];
			lastPos[1] = array[3];
			skidding = 1;
		}
		else
		{
			array[1] = lastPos[0];
			array[0] = lastPos[1];
			ref Vector3 reference5 = ref array[2];
			Vector3 point5 = hit.point;
			Vector3 eulerAngles13 = base.transform.eulerAngles;
			float x5 = eulerAngles13.x;
			Vector3 eulerAngles14 = base.transform.eulerAngles;
			float y5 = eulerAngles14.y;
			Vector3 eulerAngles15 = base.transform.eulerAngles;
			reference5 = point5 + Quaternion.Euler(x5, y5, eulerAngles15.z) * new Vector3(0f - markWidth, 0.01f, 0f);
			ref Vector3 reference6 = ref array[3];
			Vector3 point6 = hit.point;
			Vector3 eulerAngles16 = base.transform.eulerAngles;
			float x6 = eulerAngles16.x;
			Vector3 eulerAngles17 = base.transform.eulerAngles;
			float y6 = eulerAngles17.y;
			Vector3 eulerAngles18 = base.transform.eulerAngles;
			reference6 = point6 + Quaternion.Euler(x6, y6, eulerAngles18.z) * new Vector3(markWidth, 0.01f, 0f);
			lastPos[0] = array[2];
			lastPos[1] = array[3];
		}
		array2[0] = 0;
		array2[1] = 1;
		array2[2] = 2;
		array2[3] = 2;
		array2[4] = 3;
		array2[5] = 0;
		mesh.vertices = array;
		mesh.triangles = array2;
		mesh.RecalculateNormals();
		mesh.uv = new Vector2[4]
		{
			new Vector2(1f, 0f),
			new Vector2(0f, 0f),
			new Vector2(0f, 1f),
			new Vector2(1f, 1f)
		};
		meshFilter.mesh = mesh;
		gameObject.GetComponent<Renderer>().material = skidMaterial;
		gameObject.AddComponent<destroyAfter>();
	}

	private void BumpPlay(float SuspensionStress)
	{
		float num = Mathf.InverseLerp(bumpMinForce, bumpMaxForse, SuspensionStress);
		if (num > 0f)
		{
			bumpPitch = num;
			bumpVolume = Mathf.Lerp(bumpMinVolume, bumpMaxVolume, num);
			bump = true;
		}
		else
		{
			bump = false;
		}
	}
}
